package com.sww.pj.sys.service.impl;

import java.util.List;

import com.sww.pj.common.utils.AssertUtils;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sww.pj.common.annotation.RequiredLog;
import com.sww.pj.common.exception.ServiceException;
import com.sww.pj.common.pojo.PageObject;
import com.sww.pj.sys.dao.SwwWzDao;
import com.sww.pj.sys.pojo.SwwWz;
import com.sww.pj.sys.service.SwwWzService;
@Service
public class SwwWzServiceImpl implements SwwWzService {
	@Autowired
	private SwwWzDao swwWzDao;
	@Override
	public List<SwwWz> findWz() {
		return swwWzDao.findWz();
	}
	@Override
	public List<SwwWz> findSwwWz() {

		return null;
	}
	@Override
	@RequiresPermissions("sww:wzxx:find")
	@RequiredLog("查询物资")
	public PageObject<SwwWz> findPageObjects(String wzname, Long pageCurrent) {
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("当前页码值无效");
		//2.查询总记录数并进行校验
		long rowCount=swwWzDao.getRowCount(wzname);
		if(rowCount==0)
			throw new ServiceException("没有找到对应记录");
		//3.查询当前页记录
		int pageSize=10;
		Long startIndex=(long)(pageCurrent-1)*pageSize;
		List<SwwWz> records=swwWzDao.findPageObjects(wzname,startIndex, pageSize);
		return new PageObject<>(rowCount, records, pageSize, pageCurrent);

	}
	@Override
	@RequiresPermissions("sww:wzxx:delete")
	@RequiredLog("删除物资")
	public int deleteObjects(Integer... ids) {
		if(ids==null||ids.length==0)
			throw new IllegalArgumentException("请输入id的值");
		int rows= swwWzDao.deleteObjects(ids);
		if(rows==0)
			throw new ServiceException("记录可能已经不存在");
		return rows;
	}

	@Override
	public SwwWz findObjectById(Integer wzid) {
		AssertUtils.isArgValid(wzid==null||wzid<1,"id值无效");
		//2.查询用户以及用户对应的信息
		SwwWz wzxx=swwWzDao.findObjectById(wzid);
		AssertUtils.isServiceValid(wzxx==null, "记录可能已经不存在");


		return wzxx;
	}

	@Override
	@RequiresPermissions("sww:wzxx:update")
	@RequiredLog("修改物资")
	public int updateObjects(SwwWz entity) {
		return swwWzDao.updateObject(entity);
	}

	@Override
	@RequiresPermissions("sww:wzxx:add")
	@RequiredLog("添加物资")
	public int saveObject(SwwWz entity) {
		if(entity==null)
			throw new ServiceException("保存对象不能为空");
		if(StringUtils.isEmpty(entity.getWzname()))
			throw new ServiceException("物资名称不能为空");

		return swwWzDao.insertObject(entity);
	}

}
